
// components/case-imgs/index.js
const request = require('../../../utils/request')
Component({
  /**
   * 组件的属性列表
   */
  properties: {
    list: {
      type: Array,
      value: '',
      observer: function(val) {
        var that = this;
        that.setData({
          uploaderList: val,
          uploaderNum: val.length,
        })
        if(val.length === this.data.limit){
          that.setData({
            showUpload: false
          })
        }
      }
    },
    type:{
      type: Number,
      value: 1,
    },
    explain:{
      type: String,
      value: ''
    },
    limit:{
      type: Number,
      value: 5
    }
  },
  /**
   * 组件的初始数据
   */
  data: {
    uploaderList: [],
    uploaderNum: 0,
    showUpload: true,
    uploaderNowNum:0
  },
  /**
   * 组件的方法列表
   */
  methods: {
    // 删除图片
    clearImg: function(e) {
      var nowList = []; //新数据
      var uploaderList = this.data.uploaderList; //原数据
      for (let i = 0; i < uploaderList.length; i++) {
        if (i == e.currentTarget.dataset.index) {
          continue;
        } else {
          nowList.push(uploaderList[i])
          continue;
        }
      }
      this.setData({
        uploaderNum: this.data.uploaderNum - 1,
        uploaderList: nowList,
        showUpload: true
      })
      this.triggerEvent('getUploaderList', {
        uploaderList: nowList,
        key: this.data.key
      })
    },
    //展示图片
    showImg: function(e) {
      var index = e.currentTarget.dataset.index;
      var list = this.data.uploaderList;
      var urls = [];
      list.forEach(item => {
        urls.push(item.url)
      });
      wx.previewImage({
        urls: urls,
        current: urls[index]
      })
    },
    //上传图片
    upload: function(e) {
      var that = this;
      wx.chooseImage({
        count: 1, // 默认5
        sizeType: ['original', 'compressed'], // 可以指定是原图还是压缩图，默认二者都有
        sourceType: ['album', 'camera'], // 可以指定来源是相册还是相机，默认二者都有
        success: function(res) {
          // 返回选定照片的本地文件路径列表，tempFilePath可以作为img标签的src属性显示图片
          let tempFilePaths = res.tempFilePaths[0];
          that.upImgs(tempFilePaths)
        }
      })
    },
    upImgs(imgurl){
      var guid = '';
      var that = this
      var url = ''
      url = request.BASE_URL + '/api?method=images.upload'
      guid = wx.getStorageSync("token");
      wx.uploadFile({
        url: url, // oss地址
        filePath: imgurl,
        name: 'file',
        header:{
          'Authorization': guid
        },
        success: function (file) {
          var data = JSON.parse(file.data)  // 坑2：与wx.request不同的是，upload返回的是字符串格式，需要字符串对象化
          console.log(data)
          if (data.status) {
            let obj = {
              name: '',
              url: data.data.url
            }
            let uploaderList = that.data.uploaderList.concat(obj);
            if(that.data.limit === uploaderList.length){
              that.setData({
                showUpload: false
              })
            }
            that.setData({
              uploaderList: uploaderList,
              uploaderNum: uploaderList.length,
            })
            console.log(uploaderList)
            that.triggerEvent('getUploaderList', {
              uploaderList: uploaderList,
              explain: that.data.explain
            })
          }else{
            console.log('上传失败')
            wx.showToast({
              title: data.msg,
              icon: 'none'
            })
          }
        }
      })
    }
  }
})